//app.js
import {axios} from "./api/http.js";
import {getShopCarList,addShopCarList} from "./api/Url.js"
App({
  onLaunch: function () {

    var that = this
    // 展示本地存储能力
    var logs = wx.getStorageSync('logs') || []
    logs.unshift(Date.now())
    wx.setStorageSync('logs', logs)

    // 登录
    wx.login({
      success: function (res) {
          console.log(res)
           if (res.code) {
              console.log('通过login接口的code换取openid')
               wx.request({
                 url: 'https://api.weixin.qq.com/sns/jscode2session',
                 data: {
                    //填上自己的小程序唯一标识
                   appid: 'wx3adb050cd6c09ac6',
                    //填上自己的小程序的 app secret
                   secret: '9997d60c160a00719d39a51c1f572ef8',
                   grant_type: 'authorization_code',
                   js_code: res.code
                 },
                 method: 'GET',
                 header: { 'content-type': 'application/json'},
                 success: function(openIdRes){
                      console.info("登录成功返回的openId：" + openIdRes.data.openid);
                      that.globalData.openId = openIdRes.data.openid
                      axios(getShopCarList,{openId:openIdRes.data.openid},"get",(res)=>{
                        console.log(res)
                        that.globalData.shopCar=res
                      })
                 },
                 fail: function(error) {
                     console.info("获取用户openId失败");
                     console.info(error);
                 }
              })
            }
          }
      })
    // 获取用户信息
    wx.getSetting({
      success: res => {
        if (res.authSetting['scope.userInfo']) {
          // 已经授权，可以直接调用 getUserInfo 获取头像昵称，不会弹框
          wx.getUserInfo({
            success: res => {
              // 可以将 res 发送给后台解码出 unionId
              this.globalData.userInfo = res.userInfo

              // 由于 getUserInfo 是网络请求，可能会在 Page.onLoad 之后才返回
              // 所以此处加入 callback 以防止这种情况
              if (this.userInfoReadyCallback) {
                this.userInfoReadyCallback(res)
              }
            }
          })
        }
      }
    })
  },
  onHide(){
    var that = this
    console.log(that.globalData.shopCar)
    axios(addShopCarList,{params:that.globalData.shopCar},"post",(res)=>{
      console.log(res)
    })
  },
  globalData: {
    // 用户信息
    userInfo: null,
    // openID
    openId:"",
    // 购物车数组
    shopCar:[],
    //选中的数组(用来删除用)
    checkedShopCar:[],
    //地址列表
    orderList:[],
    //默认地址
    orderTitle:"",
    //订单提交后结算页面需要数组
    orderFrom:[],
    //新页面 
    ploceorcar:[],
    //
    zhl:[]
  }
})